package com.cloud.flowable.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cloud.common.domain.Result;
import com.cloud.entity.vo.TaskParam;
import com.cloud.flowable.service.IWfTaskService;
import com.cloud.security.aop.RequestPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Tag(name = "工作流任务管理")
@RestController
@RequestMapping("/flowable/task")
public class WfTaskController {

    @Autowired
    private IWfTaskService flowTaskService;

    @Operation(summary = "取消申请")
    @RequestPermissions("flowable:process:cancel")
    @PostMapping(value = "/stopProcess")
    public Result stopProcess(@RequestBody TaskParam taskParam) {
        flowTaskService.stopProcess(taskParam);
        return Result.success("取消申请");
    }

    @Operation(summary = "撤回流程")
    @RequestPermissions("flowable:process:revoke")
    @PostMapping(value = "/revokeProcess")
    public Result revokeProcess(@RequestBody TaskParam taskParam) {
        flowTaskService.revokeProcess(taskParam);
        return Result.success("撤回流程");
    }

    @Operation(summary = "获取流程变量")
    @RequestPermissions("flowable:process:query")
    @GetMapping(value = "/processVariables")
    public Result processVariables(String taskId) {
        return Result.success("获取流程变量", flowTaskService.getProcessVariables(taskId));
    }

    @Operation(summary = "审批任务")
    @RequestPermissions("flowable:process:approval")
    @PostMapping(value = "/complete")
    public Result complete(@RequestBody TaskParam taskParam) {
        flowTaskService.complete(taskParam);
        return Result.success("审批任务成功");
    }

    @Operation(summary = "拒绝任务")
    @RequestPermissions("flowable:process:reject")
    @PostMapping(value = "/reject")
    public Result taskReject(@RequestBody TaskParam taskParam) {
        flowTaskService.taskReject(taskParam);
        return Result.success("拒绝任务成功");
    }

    @Operation(summary = "退回任务")
    @RequestPermissions("flowable:process:return")
    @PostMapping(value = "/return")
    public Result taskReturn(@RequestBody TaskParam taskParam) {
        flowTaskService.taskReturn(taskParam);
        return Result.success("退回任务成功");
    }

    @Operation(summary = "获取所有可回退的节点")
    @RequestPermissions("flowable:process:query")
    @PostMapping(value = "/returnList")
    public Result findReturnTaskList(@RequestBody TaskParam taskParam) {
        return Result.success("查询成功", flowTaskService.findReturnTaskList(taskParam));
    }

    @Operation(summary = "删除任务")
    @RequestPermissions("flowable:process:approval")
    @PostMapping(value = "/delete")
    public Result delete(@RequestBody TaskParam taskParam) {
        flowTaskService.deleteTask(taskParam);
        return Result.success("删除任务成功");
    }

    @Operation(summary = "认领/签收任务")
    @RequestPermissions("flowable:process:claim")
    @PostMapping(value = "/claim")
    public Result claim(@RequestBody TaskParam taskParam) {
        flowTaskService.claim(taskParam);
        return Result.success("认领/签收任务,成功");
    }

    @Operation(summary = "取消认领/签收任务")
    @RequestPermissions("flowable:process:claim")
    @PostMapping(value = "/unClaim")
    public Result unClaim(@RequestBody TaskParam taskParam) {
        flowTaskService.unClaim(taskParam);
        return Result.success("签收任务/取消认领,成功");
    }

    @Operation(summary = "委派任务")
    @RequestPermissions("flowable:process:delegate")
    @PostMapping(value = "/delegate")
    public Result delegate(@RequestBody TaskParam taskParam) {
        if (ObjectUtil.hasNull(taskParam.getTaskId(), taskParam.getUserId())) {
            return Result.error("参数错误");
        }
        flowTaskService.delegateTask(taskParam);
        return Result.success("委派成功");
    }

    @Operation(summary = "转办任务")
    @RequestPermissions("flowable:process:transfer")
    @PostMapping(value = "/transfer")
    public Result transfer(@RequestBody TaskParam taskParam) {
        if (ObjectUtil.hasNull(taskParam.getTaskId(), taskParam.getUserId())) {
            return Result.error("参数错误");
        }
        flowTaskService.transferTask(taskParam);
        return Result.success("转办成功");
    }

    @Operation(summary = "生成流程图")
    @GetMapping("/diagram")
    public void genProcessDiagram(HttpServletResponse response, String processId) {
        InputStream inputStream = flowTaskService.diagram(processId);
        OutputStream os = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(inputStream);
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
